package com.mqb.n8;

import com.mqb.n2.util.Sleeper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.Service;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author mqb
 * @date 2021/5/3 20:47
 */
@Slf4j(topic = "c.TestCountdownLatch")
public class TestCountdownLatch {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newCachedThreadPool();
        RestTemplate restTemplate = new RestTemplate();

        Future<Map<String, Object>> f1 = pool.submit(() -> {
            Map<String, Object> response = restTemplate.getForObject("http://localhost:8080/order/{1}", Map.class, 1);
            log.debug("end order: {}", response);
            return response;
        });

        Future<Map<String, Object>> f2 = pool.submit(() -> {
            Map<String, Object> response = restTemplate.getForObject("http://localhost:8080/product/{1}", Map.class, 1);
            log.debug("end order: {}", response);
            return response;
        });

        Future<Map<String, Object>> f3 = pool.submit(() -> {
            Map<String, Object> response = restTemplate.getForObject("http://localhost:8080/logistics/{1}", Map.class, 1);
            log.debug("end order: {}", response);
            return response;
        });
        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        log.debug("执行结束");
    }

    private static void test3() throws InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        CountDownLatch cdl = new CountDownLatch(10);

        Random random = new Random();
        String[] all = new String[10];

        for (int j = 0; j < 10; j++) {
            int order = j;
            pool.submit(() -> {
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    all[order] = i + "%";
                    // 让下一次打印覆盖前一次
                    System.out.print("\r" + Arrays.toString(all));
                }
                cdl.countDown();
            });
        }

        cdl.await();
        System.out.println("\n游戏开始");
        pool.shutdown();
    }

    private static void test2() {
        CountDownLatch cdl = new CountDownLatch(3);

        ExecutorService pool = Executors.newFixedThreadPool(4);
        pool.submit(() -> {
            Sleeper.sleep(3);
            log.debug("我也走了...");
            cdl.countDown();
        });

        pool.submit(() -> {
            log.debug("我走了");
            cdl.countDown();
        });

        pool.submit(() -> {
            Sleeper.sleep(1);
            log.debug("我走了.");
            cdl.countDown();
        });

        pool.submit(() -> {
            log.debug("waiting");
            try {
                cdl.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("都走了，我关门了");
        });
    }

    private static void method1() throws InterruptedException {
        CountDownLatch cdl = new CountDownLatch(3);
        log.debug("等待3位同学离开...");

        new Thread(() -> {
            Sleeper.sleep(3);
            log.debug("我也走了...");
            cdl.countDown();
        }, "小明").start();

        new Thread(() -> {
            log.debug("我走了");
            cdl.countDown();
        }, "小红").start();

        new Thread(() -> {
            Sleeper.sleep(1);
            log.debug("我走了.");
            cdl.countDown();
        }, "李雷").start();

        log.debug("waiting");
        cdl.await();
        log.debug("都走了，我关门了");
    }
}
